home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13844 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: A weird thing about printf()
  5. Date: 10 Apr 1996 15:23:18 GMT
  6. Organization: Borland International
  7. Message-ID: <4kgjp6$6lm@druid.borland.com>
  8. References: <4kflr2$5if@dewey.csun.edu>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4kflr2$5if@dewey.csun.edu>, kc44097@csun.edu says...
  15. >
  16. >
  17. >  
  18. >  The program :
  19. >
  20. >#include <stdio.h>
  21. >
  22. >int answer;
  23. >main()
  24. >{
  25. >  answer=2+2;
  26. >  printf("The answer is %d\n");
  27. >  return 0;
  28. >}
  29. >
  30. >  In printf(),I lost "answer" in the end,but it works,and give me the
  31. >result 0.I wonder how the compiler handle this condition.Also,is this
  32. >allow by C?(by this, I mean is this a leagal usage in language itself,
  33. >or just a mistake cause by the compiler?)
  34.  
  35. Yes, this is legal. printf() takes a variable number of arguments, and the 
  36. compiler is not required to check for consistency between the string you pass 
  37. as the first argument and the number of arguments you actually pass. printf 
  38. looked at the place where the next argument would have been. This will usually 
  39. result in nonsensical output.
  40.  
  41.